home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / misc / attacks.lzh / Attacks / Sources / history.mod < prev    next >
Text File  |  1992-04-15  |  13KB  |  274 lines

  1. IMPLEMENTATION MODULE history;
  2.  
  3. (*$R-*) (* range checking OFF *)
  4.  
  5. FROM header
  6.   IMPORT   boardtype, boardrange, histnodetype, histnodeptrtype, historytype,
  7.            playertype;
  8. FROM Storage
  9.   IMPORT   ALLOCATE, DEALLOCATE;
  10. FROM TermInOut
  11.   IMPORT   WriteString, WriteLn, WriteCard;
  12.  
  13.  
  14. (*************************************************************************)
  15.  
  16. PROCEDURE InitHistory (VAR history : historytype;
  17.                        board : boardtype;  player : playertype) : BOOLEAN;
  18.  
  19. (*   This takes an uninitialized variable of history type and sets all    *)
  20. (* the appropriate things to an initial value.                            *)
  21. (*                                                                        *)
  22. (*   INPUT                                                                *)
  23. (*            history           Variable of history type.  It should be   *)
  24. (*                              unused.                                   *)
  25. (*                                                                        *)
  26. (*            board             This board should represent the current   *)
  27. (*                              board state.                              *)
  28. (*                                                                        *)
  29. (*            player            And similarly for the player.             *)
  30. (*                                                                        *)
  31. (*   OUTPUT                                                               *)
  32. (*            history           Same variable.  It will be modified so    *)
  33. (*                              that it will reflect an initial state.    *)
  34. (*                                                                        *)
  35. (*            The function will return TRUE only if it can properly in-   *)
  36. (*            itialize the variable.  If something goes wrong -> FALSE.   *)
  37. VAR
  38.   ptr : histnodeptrtype;
  39.  
  40. BEGIN
  41.   ALLOCATE(ptr, SIZE(histnodetype));
  42.   IF ptr = NIL THEN
  43.      RETURN FALSE;
  44.      END;
  45.   ptr^.board := board;
  46.   ptr^.turn := player;
  47.   ptr^.next := NIL;
  48.   ptr^.previous := NIL;
  49.   history.currentmove := ptr;
  50.   history.nummoves := 1;
  51.   RETURN TRUE;
  52. END InitHistory;
  53.  
  54. (**************************************************************************)
  55.  
  56. PROCEDURE AddToHistory (VAR history : historytype;
  57.                             board : boardtype;  player : playertype)
  58.                          : BOOLEAN;
  59.  
  60. (*   This takes an initialized variable of history type and adds the      *)
  61. (* board to it.  NOTE: it is up to the caller to maintain all the proper  *)
  62. (* changes to its own state.  This just keeps track of the moves.         *)
  63. (*                                                                        *)
  64. (*   INPUT                                                                *)
  65. (*            history           Variable of history type.  It definately  *)
  66. (*                              should be initialized.                    *)
  67. (*                                                                        *)
  68. (*            board             A boardtype.  It is what will be added    *)
  69. (*                              to the history.                           *)
  70. (*                                                                        *)
  71. (*            player            Whose turn it was to move at this board's *)
  72. (*                              configuration.                            *)
  73. (*                                                                        *)
  74. (*   OUTPUT                                                               *)
  75. (*            history           Same variable.  It will be modified so    *)
  76. (*                              that it will now hold the new board.      *)
  77. (*                                                                        *)
  78. (*            The function will return TRUE only if it can properly add   *)
  79. (*            the new board to the history.  If something goes wrong,     *)
  80. (*            then this will try to return the original history and       *)
  81. (*            return FALSE.                                               *)
  82.  
  83. VAR
  84.   ptr : histnodeptrtype;
  85.  
  86. BEGIN
  87.            (* This removes all the moves off the top *)
  88.   WHILE history.currentmove^.next # NIL DO
  89.      ptr := history.currentmove^.next;
  90.      history.currentmove^.next := ptr^.next;
  91.      DEALLOCATE(ptr, SIZE(histnodetype));
  92.      DEC(history.nummoves);
  93.      END;
  94.  
  95.            (* Here we add the new move to the data struct *)
  96.   ALLOCATE(ptr, SIZE(histnodetype));
  97.   IF ptr = NIL THEN
  98.      RETURN FALSE;
  99.      END;
  100.   ptr^.board := board;
  101.   ptr^.turn := player;
  102.  
  103.   ptr^.previous := history.currentmove;
  104.   ptr^.next := NIL;
  105.   history.currentmove^.next := ptr;
  106.   history.currentmove := ptr;
  107.   INC (history.nummoves);
  108.   RETURN TRUE;
  109. END AddToHistory;
  110.  
  111. (**************************************************************************)
  112.  
  113. PROCEDURE PopHistory (VAR history : historytype;
  114.                       VAR board : boardtype;  VAR player : playertype)
  115.                          : BOOLEAN;
  116.  
  117. (*   This takes an initialized variable of history type and removes the   *)
  118. (* most recent move from it.  The result is put into the variable board.  *)
  119. (* If there are no boards in the current history (ie, the history list    *)
  120. (* is empty) then the function will return FALSE.                         *)
  121. (*                                                                        *)
  122. (*   INPUT                                                                *)
  123. (*            history           Variable of history type.  It definately  *)
  124. (*                              should be initialized.                    *)
  125. (*                                                                        *)
  126. (*            board             A boardtype.  Initially it is garbage.    *)
  127. (*                                                                        *)
  128. (*            player            A playertype.  Also garbage at the start. *)
  129. (*                                                                        *)
  130. (*   OUTPUT                                                               *)
  131. (*            history           Same variable.  It will be modified so    *)
  132. (*                              that it no longer holds the returned      *)
  133. (*                              board.                                    *)
  134. (*                                                                        *)
  135. (*            board             This will hold the board that is re-      *)
  136. (*                              turned.  If the history contains no pre-  *)
  137. (*                              vious boards, then this variable is un-   *)
  138. (*                              changed.                                  *)
  139. (*                                                                        *)
  140. (*            player            Holds the player whose turn it is for the *)
  141. (*                              returned board.                           *)
  142. (*                                                                        *)
  143. (*                                                                        *)
  144. (*            The function will return TRUE only if it can properly pop   *)
  145. (*            a board from it.  If the history is empty, then the func-   *)
  146. (*            returns FALSE.                                              *)
  147.  
  148.  
  149. BEGIN
  150.   IF history.currentmove^.previous = NIL THEN
  151.      RETURN FALSE;
  152.      END;
  153.   history.currentmove := history.currentmove^.previous;
  154.   board := history.currentmove^.board;
  155.   player := history.currentmove^.turn;
  156.   RETURN TRUE;
  157. END PopHistory;
  158.  
  159.  
  160. (**************************************************************************)
  161. PROCEDURE UpHistory (VAR history : historytype;  VAR board : boardtype;
  162.                      VAR player : playertype) : BOOLEAN;
  163.  
  164. (*   This is used to REDO moves.  It is called and works very much like   *)
  165. (* PopHistory.                                                            *)
  166. (*                                                                        *)
  167. (*   INPUT                                                                *)
  168. (*            history           Variable of history type.  It definately  *)
  169. (*                              should be initialized.                    *)
  170. (*                                                                        *)
  171. (*            board             A boardtype.  Initially it is garbage.    *)
  172. (*                                                                        *)
  173. (*            player            A playertype.  Also garbage at the start. *)
  174. (*                                                                        *)
  175. (*   OUTPUT                                                               *)
  176. (*            history           Same variable.  It may be modified, but   *)
  177. (*                              don't worry about it.                     *)
  178. (*                                                                        *)
  179. (*            board             This will hold the board that is re-      *)
  180. (*                              turned.  If the history contains no next  *)
  181. (*                              boards, then this variable is unchange.   *)
  182. (*                                                                        *)
  183. (*            player            Holds the player whose turn it is for the *)
  184. (*                              returned board.                           *)
  185. (*                                                                        *)
  186. (*                                                                        *)
  187. (*            The function will return TRUE only if it can properly make  *)
  188. (*            a board from it.  If there is no further boards to go Up,   *)
  189. (*            it returns FALSE.                                           *)
  190.  
  191. BEGIN
  192.   IF history.currentmove^.next = NIL THEN
  193.      RETURN FALSE;
  194.      END;
  195.  
  196.   history.currentmove := history.currentmove^.next;
  197.   board := history.currentmove^.board;
  198.   player := history.currentmove^.turn;
  199.   RETURN TRUE;
  200.  
  201. END UpHistory;
  202.  
  203. (**************************************************************************)
  204. PROCEDURE NewHistory (VAR history : historytype;  board : boardtype;
  205.                       player : playertype);
  206.  
  207. (*   Given an already initialized history, this resets it to make a brand *)
  208. (* new history structure with the current board and player set in it.     *)
  209. (*                                                                        *)
  210. (*   INPUT                                                                *)
  211. (*            history           Variable of history type.  It definately  *)
  212. (*                              should be initialized, though not neces-  *)
  213. (*                              sarily full of boards.                    *)
  214. (*                                                                        *)
  215. (*            board             This board should represent the current   *)
  216. (*                              board state.                              *)
  217. (*                                                                        *)
  218. (*            player            And similarly for the player.             *)
  219. (*                                                                        *)
  220. (*   OUTPUT                                                               *)
  221. (*            history           Same variable.  It will be modified so    *)
  222. (*                              that it now is devoid of boards save one. *)
  223.  
  224. VAR
  225.   ptr : histnodeptrtype;
  226.  
  227. BEGIN
  228.            (* This removes all the moves off the top *)
  229.   WHILE history.currentmove^.next # NIL DO
  230.      ptr := history.currentmove^.next;
  231.      history.currentmove^.next := ptr^.next;
  232.      DEALLOCATE(ptr, SIZE(histnodetype));
  233.      DEC(history.nummoves);
  234.      END;
  235.  
  236.            (* Removes all the moves off the bottem *)
  237.   WHILE history.currentmove^.previous # NIL DO
  238.      ptr := history.currentmove^.previous;
  239.      history.currentmove^.previous := ptr^.previous;
  240.      DEALLOCATE(ptr, SIZE(histnodetype));
  241.      DEC(history.nummoves);
  242.      END;
  243.  
  244.   history.currentmove^.board := board;
  245.   history.currentmove^.turn := player;
  246. END NewHistory;
  247.  
  248.  
  249. (**************************************************************************)
  250.  
  251. PROCEDURE CloseHistory (VAR history : historytype);
  252.  
  253. (*   This takes a variable of history type and clears all the memory and  *)
  254. (* etc associated with it.                                                *)
  255. (*                                                                        *)
  256. (*   INPUT                                                                *)
  257. (*            history           Variable of history type.                 *)
  258. (*                                                                        *)
  259. (*   OUTPUT                                                               *)
  260. (*            history           Same variable.  It will be modified and   *)
  261. (*                              should NOT be used after calling this     *)
  262. (*                              routine.                                  *)
  263. VAR
  264.   fooboard : boardtype;
  265.   fooplayer : playertype;
  266.  
  267. BEGIN
  268.   NewHistory(history, fooboard, fooplayer);
  269.   DEALLOCATE(history.currentmove, SIZE(histnodetype));
  270. END CloseHistory;
  271.  
  272. (*************************************************)
  273. END history.
  274.